﻿Imports MICROFORMA.BEL
Imports MICROFORMA.DIL
Imports Microsoft.Practices.EnterpriseLibrary.Data
Imports System.Data.Common

Public Class EscanerDA
    Implements DIL.EscanerDI

    Public Function ListAll() As System.Data.DataTable Implements DIL.EscanerDI.ListAll

        Dim db As Database = DatabaseFactory.CreateDatabase()
        Dim query As String = "select * from Escaners Order By 1 asc" 'TODO: HardCode

        Dim dbCommand As DbCommand = db.GetSqlStringCommand(query)

        Dim EscanerDataSet As DataSet = Nothing

        EscanerDataSet = db.ExecuteDataSet(dbCommand)

        Return EscanerDataSet.Tables(0)

    End Function
    Public Function ListAllItem(ByVal IdEscaner As Integer) As System.Data.DataTable Implements DIL.EscanerDI.ListAllItem

        Dim db As Database = DatabaseFactory.CreateDatabase()
        Dim query As String = "select * from Escaners where IdEscaner=" & IdEscaner 'TODO: HardCode

        Dim dbCommand As DbCommand = db.GetSqlStringCommand(query)
        Dim EscanerDataSet As DataSet = Nothing
        EscanerDataSet = db.ExecuteDataSet(dbCommand)
        Return EscanerDataSet.Tables(0)

    End Function

    Public Function Eliminar(ByVal IdEscaner As Integer) As String Implements DIL.EscanerDI.Eliminar

        Dim db As Database = DatabaseFactory.CreateDatabase()
        Dim query As String = "delete from Escaners where IdEscaner=" & IdEscaner 

        Try
            Dim o_dbCommand As DbCommand = db.GetSqlStringCommand(query)
            db.ExecuteNonQuery(o_dbCommand)
            Return "OK"
        Catch ex As Exception
            Return ex.Message.ToString
        End Try

    End Function

    Public Function Add(ByVal escanerBE As BEL.EscanerBE) As String Implements DIL.EscanerDI.Add
        Dim db As Database = DatabaseFactory.CreateDatabase()
        Dim query As String
        Try

            query = " insert into escaners(Nombre,Descripcion,TipoArchivo,Resolucion,Duplex,Formato," _
                & " EliminarDesviacion,EliminarBlancos) values ('" & escanerBE.Nombre & "','" _
                & escanerBE.Descripcion & "','" & escanerBE.TipoArchivo & "'," & escanerBE.Resolucion & ",'" _
                & escanerBE.Duplex & "','" & escanerBE.Formato & "','" & escanerBE.EliminarDesviacion & "','" _
                & escanerBE.EliminarBlancos & "')"

            Dim o_dbCommand As DbCommand = db.GetSqlStringCommand(query)
            db.ExecuteNonQuery(o_dbCommand)

            Return "OK"

        Catch ex As Exception
            Return ex.Message.ToString
        End Try
    End Function

    Public Function Actualizar(ByVal escanerBE As BEL.EscanerBE) As String Implements DIL.EscanerDI.Actualizar
        Dim db As Database = DatabaseFactory.CreateDatabase()
        Dim query As String
        Try

            query = " update escaners " _
                & " set Nombre ='" & escanerBE.Nombre & "',Descripcion='" & escanerBE.Descripcion _
                & "',TipoArchivo='" & escanerBE.TipoArchivo _
                & "',Resolucion=" & escanerBE.Resolucion _
                & ", EliminarDesviacion='" & escanerBE.EliminarDesviacion _
                & "',EliminarBlancos='" & escanerBE.EliminarBlancos _
                & "',Duplex='" & escanerBE.Duplex _
                & "',Formato=" & escanerBE.Formato _
                & " where idEscaner=" & escanerBE.IdEscaner


            Dim o_dbCommand As DbCommand = db.GetSqlStringCommand(query)
            db.ExecuteNonQuery(o_dbCommand)

            Return "OK"

        Catch ex As Exception
            Return ex.Message.ToString
        End Try
    End Function



End Class

